package org.jeecg.modules.generator.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.generator.entity.SysGenerateTemplate;
import org.jeecg.modules.generator.service.SysGenerateTemplateService;
import org.jeecg.modules.generator.vo.SysGenerateTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.jeecg.common.aspect.annotation.AutoLog;

import java.util.List;

/**
* @Description 模板内容配置表控制器
* @Author yangfeng
* @Date 2021-06-29 18:03:59
*/
@RestController
@RequestMapping(value="generator/MoudualTemplateConfig")
@Api(tags = "模板内容配置表")
public class SysGenerateTemplateController {

    @Autowired
    private SysGenerateTemplateService sysGenerateTemplateService;

    @Autowired
    private ISysBaseAPI  sysBaseAPI;



    /**
    * 查询模板内容配置表分页数据
    * @param sysGenerateTemplateVo
    * @return
    */
    @ApiOperation(value = "模板内容配置表-分页查询")
    @AutoLog("模板内容配置表-分页查询")
    @GetMapping(value="list")
    public Result<IPage<SysGenerateTemplate>> list(SysGenerateTemplateVo sysGenerateTemplateVo){

        Result<IPage<SysGenerateTemplate>> result = new Result<>();
        QueryWrapper queryWrapper = sysGenerateTemplateVo.getQueryWrapper();
            if(StringUtils.isNotEmpty(sysGenerateTemplateVo.getName())){
                queryWrapper.like("NAME",sysGenerateTemplateVo.getName());
            }
            if(StringUtils.isNotEmpty(sysGenerateTemplateVo.getSystemId())){
                queryWrapper.like("system_id",sysGenerateTemplateVo.getSystemId());
            }
            if(StringUtils.isNotEmpty(sysGenerateTemplateVo.getStatus())){
                queryWrapper.eq("STATUS",sysGenerateTemplateVo.getStatus());
            }
        IPage<SysGenerateTemplate> pageList = sysGenerateTemplateService.page(sysGenerateTemplateVo.getPage(),queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    /**
    * 根据主键查询模板内容配置表对象数据
    * @param id
    * @return
    */
    @GetMapping(value="queryById/{id}")
    public Result queryById(@PathVariable("id")String id){

        SysGenerateTemplate sysGenerateTemplate = sysGenerateTemplateService.getById(id);
        if(sysGenerateTemplate != null){
           return Result.ok(sysGenerateTemplate);
        }else{
            return Result.error("未找到查询对象！");
        }
    }
    /**
    * 模板内容配置表新增保存
    * @param sysGenerateTemplate
    * @return
    */
    @ApiOperation(value = "模板内容配置表-新增保存")
    @AutoLog("模板内容配置表-新增保存")
    @PostMapping(value="add")
    public Result add(@RequestBody SysGenerateTemplate sysGenerateTemplate){
        sysGenerateTemplateService.save(sysGenerateTemplate);
        return Result.ok("新增模板内容配置表成功！");
    }

    /**
    * 模板内容配置表修改保存
    * @param sysGenerateTemplate
    * @return
    */
    @ApiOperation(value = "模板内容配置表-修改保存")
    @AutoLog("模板内容配置表-修改保存")
    @PutMapping(value="edit")
    public Result edit(@RequestBody SysGenerateTemplate sysGenerateTemplate){

        SysGenerateTemplate generateTemplate = sysGenerateTemplateService.getById(sysGenerateTemplate.getId());
        if(generateTemplate != null){
            List<DictModel> dictModelList = sysBaseAPI.queryDictItemsByCode("generate_file_type");
            DictModel dictModel = dictModelList.stream().filter(tempDictModel -> tempDictModel.getValue().equals(sysGenerateTemplate.getOutPutPath())).findFirst().orElse(null);
            if(dictModel != null){
                sysGenerateTemplate.setSuffixFileName(dictModel.getText());
            }
            sysGenerateTemplateService.updateById(sysGenerateTemplate);
            return Result.ok("修改模板内容配置表成功！");
        }else{
            return Result.error("修改模板内容配置表不存在！");
        }
    }
    /**
    *模板内容配置表删除
    * @param id
    * @return
    */

    @ApiOperation(value = "模板内容配置表-删除")
    @AutoLog("模板内容配置表-删除")
    @DeleteMapping(value="delete")
    public Result  delete(@RequestParam("id")String id){

        sysGenerateTemplateService.removeById(id);
        return Result.ok("删除模板内容配置表数据成功！");
    }
}
